package jiemian1;

//import com.alee.laf.WebLookAndFeel;
import gonggong.User;
import gonggong.mySql_Driver;
import user.Member;
import user.MemberManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class changepass {
    private JTextField txtid;
    private JTextField txtnewpass;
    private JTextField txtpass;
    private JButton surebtn;
    private JButton deletebtn;
    private JLabel id;
    private JLabel newpass;
    private JLabel surepass;
    public JPanel change;
    private static String userId;
    private static String newpass1;
    private static String newpass2;
    public  static JFrame frame1 = new JFrame("changepass");
    mySql_Driver mysql = new mySql_Driver();
    int count=0;
    User user[];

    public changepass() {
        surebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //与数据库进行对比判定是否有该账号，如果有将新的信息更新至数据库，如果没有，提示错误信息“当前无该账号”。。。
                boolean flag = false;
                userId=txtid.getText();
                newpass1=txtnewpass.getText();
                newpass2=txtpass.getText();
                Member msg1=new Member();
                msg1.setUserId(userId);
                Member msg=new Member();
                msg.setUserId(userId);
                msg.setNewpass1(newpass1) ;
                msg.setNewpass2(newpass2) ;
                MemberManager memberManager=new MemberManager();
                frame1.dispose();
            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //取消按钮直接关闭
                frame1.dispose();
            }
        });
    }
    public changepass(JFrame x) {
        frame1=x;
        try {
            user = mysql.read();
            count=mysql.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        surebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //与数据库进行对比判定是否有该账号，，如果没有，提示错误信息“当前无该账号，请重新输入。。。”。。。
                userId=txtid.getText();
                newpass1=txtnewpass.getText();
                newpass2=txtpass.getText();
                boolean flag =false;
                for(int i=1;i<=count;i++){
                    if(user[i].getUserId().equals(userId)){
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    JOptionPane.showMessageDialog(null,"账号不存在");
                    return ;
                }
                if(!newpass1.equals(newpass2)){
                    JOptionPane.showMessageDialog(null,"两个密码不一样");
                    return ;
                }
                mysql.changePassword(userId,newpass1);
                JOptionPane.showMessageDialog(null,"修改成功");
                //如果有，将新的信息更新至数据库，之后关闭该窗口
                frame1.dispose();
            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //取消按钮直接关闭
                frame1.dispose();
            }
        });
    }
    public static void main(String[] args) {
//        WebLookAndFeel.install ();
        frame1.setContentPane(new changepass().change);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.pack();
        frame1.setVisible(true);
    }
}
